var app = getApp();
var http = require("../../utils/request.js");
Page({
  data: {
    active:1,
    page: 1,
    size: 10,
    shopList: {},
    searchResult: {},
    searchText: '',
    nearlyShow: true,
    searchPanelShow: false,
    tipTextShow: false,
    closeImgShow: false,
  },
  onLoad: function(options) {
    var that = this;
    wx.showNavigationBarLoading();
    http.getData('shop/getNearbyShop', {
      page: this.data.page,
      size: this.data.size,
      latitude: "36.66826", //纬度
      longitude: "117.02076" //经度
    }, this.getShopList, '');
  },
  //跳转到菜单
  onMenu: function(event) {
    var id = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    var img = event.currentTarget.dataset.img;
    var address = event.currentTarget.dataset.address;
    var time = event.currentTarget.dataset.time;
    var phone = event.currentTarget.dataset.phone;
    var startTime = event.currentTarget.dataset.starttime;
    var endTime = event.currentTarget.dataset.endtime;
    wx.navigateTo({
      url: '../menu/menu?id=' + id + '&img=' + img + '&name=' + name + '&address=' + address + '&time=' + time + "&phone=" + phone + "&startTime=" + startTime + "&endTime=" + endTime,
    })
  },
  onBindFocus: function(event) {
    if (event.detail.value){
      this.setData({
        closeImgShow: true,
      })
    }
    this.setData({
      nearlyShow: false,
      searchPanelShow: true,
      tipTextShow: false
    })
  },
  onBindInput: function(event) {
    if (event.detail.value) {
      this.setData({
        closeImgShow: true,
      })
    } else {
      this.setData({
        closeImgShow: false,
      })
    }
  },
  //失去焦点或点击手机中完成时请求数据
  onBindBlur: function(event) {
    if (!event.detail.value){
      this.setData({
        nearlyShow: true,
        searchPanelShow: false,
        tipTextShow: false,
        closeImgShow: false,
      })
    }else{
      this.setData({
        searchText: event.detail.value,
      })
      http.getData('shop/serachNearbyShop', {
        serach: this.data.searchText,
        page: this.data.page,
        size: this.data.size,
        latitude: "36.66826", //纬度
        longitude: "117.02076" //经度
      }, this.getSearchResult, '');
    }
  },
  onCanelImgTap: function(event) {
    this.setData({
      nearlyShow: true,
      searchPanelShow: false,
      tipTextShow: false,
      closeImgShow:false,
      searchResult: {},
      searchText:""
    })
    
    console.log(this.data.searchText);
  },
  //附近商家列表
  getShopList: function(data) {
    var that = this;
    that.setData({
      shopList: data
    })
    wx.hideNavigationBarLoading();
  },
  //搜索结果
  getSearchResult: function(data) {
    var that = this;
    if (!data.length) {
      that.setData({
        nearlyShow: false,
        searchPanelShow: false,
        tipTextShow: true,
      })
    } else {
      that.setData({
        searchResult: data
      })
    }
  },
  //上拉触底加载更多
  onReachBottom: function(event) {
    wx.showNavigationBarLoading();
    http.getData('shop/getNearbyShop', {
      page: this.data.page,
      size: this.data.size += 10,
      latitude: "36.66826",
      longitude: "117.02076"
    }, this.getShopList, '');
  },
  //下拉刷新
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading();
    http.getData('shop/getNearbyShop', {
      page: this.data.page = 1,
      size: this.data.size = 10,
      latitude: "36.66826",
      longitude: "117.02076"
    }, this.getShopList, '');
    wx.stopPullDownRefresh();
  }
})